package cn.jly.spring.mybatis.service.impl;

import cn.jly.spring.mybatis.bean.Employee;
import cn.jly.spring.mybatis.mapper.EmployeeMapper;
import cn.jly.spring.mybatis.service.EmployeeService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lanyangji
 * @description
 * @date 2020/11/18 下午 4:36
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private SqlSession sqlSession;

    @Override
    public List<Employee> getAll() {
        return employeeMapper.selectList();
    }

    @Override
    public long getCount() {
        return employeeMapper.selectCount();
    }

    @Override
    public int batchAdd(List<Employee> employees) {
        int i = 0;

        final EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);
        for (Employee employee : employees) {
            final int insert = mapper.insert(employee);
            i += insert;
        }

        return i;
    }
}
